package com.onstar.cn.api;

import com.google.gson.Gson;
import com.onstar.cn.api.framework.ClazzInjectUtils;
import com.onstar.cn.api.framework.annotation.Path;
import com.onstar.cn.api.framework.annotation.PathParams;
import com.onstar.cn.api.framework.annotation.RequestMapping;
import com.onstar.cn.chargepile.entity.ChargePileReqEntity;
import com.onstar.cn.framework.network.entity.RequestEntity;
import com.onstar.cn.framework.utils.GsonHelper;
import com.onstar.cn.mag.publicService.H5DispatcherEntity;

import java.net.URLEncoder;

/**
 * Created by 7828wc on 2016/4/12.
 */
@Path("/msp/api/v3/thirdparty")
public class ThirdpartyService {
    private String serverURL;

    /**
     * @author 7828wc
     * @date 2015年09月08日
     */
    public ThirdpartyService(String serverURL) {
        this.serverURL = serverURL;
    }

    @RequestMapping("/traffic/violationQuery")
    public RequestEntity violationQuery(String vin, String plateNumber, String engineNumber,
                                        RequestEntity requestEntity) {
        if (requestEntity == null) {
            requestEntity = new RequestEntity();
        }
        try {
            String url = ClazzInjectUtils.injectUrl(getClass(), "violationQuery", vin+"", plateNumber+"", engineNumber+"", requestEntity);
            requestEntity.setUrl(serverURL + url + "?plateNumber=" + URLEncoder.encode(plateNumber, "UTF-8") + "&engineNumber=" + engineNumber + "&vin=" + vin);
        } catch (Exception e) {
            requestEntity = null;
        }
        return requestEntity;
    }

    @RequestMapping("/traffic/restrictionQuery")
    public RequestEntity restrictionQuery(String city,
                                          RequestEntity requestEntity) {
        if (requestEntity == null) {
            requestEntity = new RequestEntity();
        }
        try {
            String url = ClazzInjectUtils.injectUrl(getClass(), "restrictionQuery", city, requestEntity);
            requestEntity.setUrl(serverURL + url + "?city=" + URLEncoder.encode(city, "UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
            return requestEntity;
        }
        return requestEntity;
    }

    @RequestMapping("/h5/redirect")
    public RequestEntity h5Redirect(String partner_id, String appVersion,RequestEntity requestEntity) {
        if (requestEntity == null) {
            requestEntity = new RequestEntity();
        }
        try {
            String url = ClazzInjectUtils.injectUrl(getClass(), "h5Redirect", partner_id, appVersion, requestEntity);
            requestEntity.setUrl(serverURL + url + "?partner_id=" + URLEncoder.encode(partner_id, "UTF-8") + "&app=" + appVersion);
        } catch (Exception e) {
            e.printStackTrace();
            return requestEntity;
        }
        return requestEntity;
    }

    @RequestMapping("/h5/dispatch")
    public RequestEntity h5Dispatcher(RequestEntity requestEntity, H5DispatcherEntity h5DispatcherEntity) {
        if (requestEntity == null) {
            requestEntity = new RequestEntity();
        }
        try {
            String url = ClazzInjectUtils.injectUrl(getClass(), "h5Dispatcher",requestEntity,h5DispatcherEntity);
            requestEntity.setUrl(serverURL + url);
            if(null != h5DispatcherEntity){
                requestEntity.setBody(new Gson().toJson(h5DispatcherEntity));
            }
        } catch (Exception e) {
            e.printStackTrace();
            return requestEntity;
        }
        return requestEntity;
    }

    //    https://api-idt1.shanghaionstar.com:1443/msp/api/v3/thirdparty/h5/redirect/mro?partner_id=xxx
    @RequestMapping("/h5/redirect/mro")
    public RequestEntity h5Redirect(String partner_id,RequestEntity requestEntity) {
        if (requestEntity == null) {
            requestEntity = new RequestEntity();
        }
        try {
            String url = ClazzInjectUtils.injectUrl(getClass(), "h5Redirect", partner_id, requestEntity);
            requestEntity.setUrl(serverURL + url + "?partner_id=" + URLEncoder.encode(partner_id, "UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
            return requestEntity;
        }
        return requestEntity;
    }


//    get/msp/api/v3/thirdparty/h5/redirect?partner_id=yhouse&app={app}
    @RequestMapping("/h5/redirect")
    public RequestEntity yhouseQuery(String partnerId,String app,
                                          RequestEntity requestEntity) {
        if (requestEntity == null) {
            requestEntity = new RequestEntity();
        }
        try {
            String url = ClazzInjectUtils.injectUrl(getClass(), "yhouseQuery", partnerId,app,requestEntity);
            requestEntity.setUrl(serverURL + url + "?partner_id=" + URLEncoder.encode(partnerId, "UTF-8")
                    +"&app="+URLEncoder.encode(app, "UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
            return requestEntity;
        }
        return requestEntity;
    }



    //获取充电桩列表数据
    @RequestMapping("/charge_station/stations")
    public RequestEntity getChargeList(RequestEntity requestEntity, ChargePileReqEntity chargePileReqEntity,
                                       @PathParams("pageNumber") Integer pageNumber){
        if (requestEntity == null) {
            requestEntity = new RequestEntity();
        }
        String url = ClazzInjectUtils.injectUrl(getClass(), "getChargeList", requestEntity,chargePileReqEntity,pageNumber);
        if(null != chargePileReqEntity){
            requestEntity.setBody(GsonHelper.toJson(chargePileReqEntity));
        }
        String fullUrl = serverURL + url + "?pageNumber=" + pageNumber;
//        fullUrl = fullUrl.replace("/thirdparty/","/");
        requestEntity.setUrl(fullUrl);
        return  requestEntity;
    }
}
